﻿using B.S.BasicData.Api.Write.Application.Command.User;
using B.S.BasicData.Domain.Entity;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Write.Application.Handler.User
{
    public class DeleteRoleHandler : IRequestHandler<DeleteRoleCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<RoleModel> _roleRepository;

        public DeleteRoleHandler(IBaseRepository<RoleModel> roleRepository)
        {
            _roleRepository = roleRepository;
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<int>> Handle(DeleteRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var role = _roleRepository.GetAll().Where(x => x.IsDelete == false && x.Id == request.Id).FirstOrDefault();
            if (role != null)
            {
                role.IsDelete = true;
            }
            var res = _roleRepository.Update(role);
            if (res > 0)
            {
                result.Code = StatusEnum.OK;
                result.Data = res;
                result.Message = "删除成功";
            }
            else
            {
                result.Code = StatusEnum.Fail;
                result.Data = res;
                result.Message = "删除失败";
            }
            return Task.FromResult(result);


        }
    }
}
